其他
ptmalloc代码研究
struct malloc_state
{
__libc_lock_define (, mutex); //锁
int flags;
int have_fastchunks; //如果fastbin不为空,这个字段就被置位
/* Fastbins */
mfastbinptr fastbinsY[NFASTBINS];
mchunkptr top; //指向top chunk
mchunkptr last_remainder; //指向切割后剩下的last reminder
/* Normal bins packed as described above */
mchunkptr bins[NBINS * 2 - 2];
unsigned int binmap[BINMAPSIZE]; //bin的位图
struct malloc_state *next;
struct malloc_state *next_free;
INTERNAL_SIZE_T attached_threads; //引用当前arena的线程数量
INTERNAL_SIZE_T system_mem; //记录当前一个获取的系统内存
INTERNAL_SIZE_T max_system_mem;
};
static struct malloc_state main_arena = //main
{
.mutex = _LIBC_LOCK_INITIALIZER,
.next = &main_arena,
.attached_threads = 1
};
#define request2size(req) \
(((req) + SIZE_SZ + MALLOC_ALIGN_MASK < MINSIZE) ? \
MINSIZE : \
((req) + SIZE_SZ + MALLOC_ALIGN_MASK) & ~MALLOC_ALIGN_MASK)
1、fastbin是malloc_chunk*类型的指针数组,是单链表的表头:
malloc_state{
....
mfastbinptr fastbinsY[NFASTBINS];
....
}
2、支持的默认最大数据空间为64字节,但实际上最大可以为80字节,步进为8字节,64位下双倍。也就是fastbin最多有10个,下标为0~9,64位也是一样。
#define set_max_fast(s) \
global_max_fast = (((s) == 0) \
? SMALLBIN_WIDTH : ((s + SIZE_SZ) & ~MALLOC_ALIGN_MASK))
if (in_smallbin_range (nb)) //只有请求的chunk为small chunk,那么剩下的才能作为last reminder
av->last_remainder = remainder;
/* Conveniently, the unsorted bin can be used as dummy top on first call */
#define initial_top(M) (unsorted_chunks (M)) //哈哈哈unsorted bin竟然可以用来作为初始化topchunk
struct malloc_par
{
/* Tunable parameters */
unsigned long trim_threshold; //设置topchunk的最大值,超过这个值就要把top chunk返还给操作系统
INTERNAL_SIZE_T top_pad;
INTERNAL_SIZE_T mmap_threshold; //申请的内存超过这个值,就要使用mmap分配内存
INTERNAL_SIZE_T arena_test;
INTERNAL_SIZE_T arena_max;
/* Memory map support */
int n_mmaps; //当前mmap的数量
int n_mmaps_max; //mmap数量的最大值
int max_n_mmaps;
/* the mmap_threshold is dynamic, until the user sets
it manually, at which point we need to disable any
dynamic behavior. */
int no_dyn_threshold;
/* Statistics */
INTERNAL_SIZE_T mmapped_mem; //当前mmap的总大小
INTERNAL_SIZE_T max_mmapped_mem;
/* First address handed out by MORECORE/sbrk. */
char *sbrk_base;
#if USE_TCACHE
/* Maximum number of buckets to use. */
size_t tcache_bins;
size_t tcache_max_bytes;
/* Maximum number of chunks in each bucket. */
size_t tcache_count;
/* Maximum number of chunks to remove from the unsorted list, which
aren't used to prefill the cache. */
size_t tcache_unsorted_limit;
#endif
};
/* There is only one instance of the malloc parameters. */
static struct malloc_par mp_ =
{
.top_pad = DEFAULT_TOP_PAD,
.n_mmaps_max = DEFAULT_MMAP_MAX,
.mmap_threshold = DEFAULT_MMAP_THRESHOLD, //128KB,超过128KB的请求就要使用mmap
.trim_threshold = DEFAULT_TRIM_THRESHOLD,
#define NARENAS_FROM_NCORES(n) ((n) * (sizeof (long) == 4 ? 2 : 8))
.arena_test = NARENAS_FROM_NCORES (1)
#if USE_TCACHE
,
.tcache_count = TCACHE_FILL_COUNT,
.tcache_bins = TCACHE_MAX_BINS,
.tcache_max_bytes = tidx2usize (TCACHE_MAX_BINS-1),
.tcache_unsorted_limit = 0 /* No limit. */
#endif
};
TRIM_FASTBINS controls whether free() of a very small chunk can
immediately lead to trimming. Setting to true (1) can reduce memory
footprint, but will almost always slow down programs that use a lot
of small chunks.
Define this only if you are willing to give up some speed to more
aggressively reduce system-level memory footprint when releasing
memory in programs that use many small chunks. You can get
essentially the same effect by setting MXFAST to 0, but this can
lead to even greater slowdowns in programs using many small chunks.
TRIM_FASTBINS is an in-between compile-time option, that disables
only those chunks bordering topmost memory from being placed in
fastbins.
1、如果在fastbin范围内就优先释放到fastbin
2、否则就尝试前后合并合:
a、并后的chunk靠近top chunk,那就并到top chunk;
b、合并后的chunk不靠近top chunk,那就放到unsorted bin;
看雪ID:极目楚天舒
https://bbs.pediy.com/user-741085.htm
推荐文章++++
好书推荐